home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Communication / NewsBase / Source / INNTP.m < prev    next >
Text File  |  1993-01-12  |  5KB  |  185 lines

  1. /*$Copyright:
  2.  * Copyright (C) 1992.5.22. Recruit Co.,Ltd. 
  3.  * Institute for Supercomputing Research
  4.  * All rights reserved.
  5.  * NewsBase  by ISR, Kazuto MIYAI, Gary ARAKAKI, Katsunori SUZUKI, Kok-meng Lue
  6.  *
  7.  * You may freely copy, distribute and reuse the code in this program under 
  8.  * following conditions.
  9.  * - to include this notice in the source code, if it is to be distributed 
  10.  *   with source code.
  11.  * - to add the file named "COPYING" within the code, which shall include 
  12.  *   GNU GENERAL PUBLIC LICENSE(*).
  13.  * - to display an acknowledgement in binary code as follows: "This product
  14.  *   includes software developed by Recruit Co.,Ltd., ISR."
  15.  * - to display a notice which shall state that the users may freely copy,
  16.  *   distribute and reuse the code in this program under GNU GENERAL PUBLIC
  17.  *   LICENSE(*)
  18.  * - to indicate the way to access the copy of GNU GENERAL PUBLIC LICENSE(*)
  19.  *
  20.  *   (*)GNU GENERAL PUBLIC LICENSE is stored in the file named COPYING
  21.  * 
  22.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  23.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  24.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  25. $*/
  26. /*
  27.  *    INNTP
  28.  */
  29.  
  30.  
  31. #import "INNTP.h"
  32.  
  33. #import <string.h>
  34. #import <stdlib.h>
  35. #import <sys/file.h>
  36. #import <sys/uio.h>
  37. #import <sys/types.h>
  38. #import <sys/socket.h>
  39. #import <streams/streams.h>
  40. #import <netdb.h>
  41. #import <netinet/in.h>
  42.  
  43. #import "response_codes.h"
  44.  
  45. #import <appkit/Panel.h>
  46. #import <objc/Storage.h>
  47.  
  48. #import "Localization.h"
  49.  
  50. #define LoStr(key)      doLocalString(NULL,key,NULL)
  51.  
  52.  
  53. @implementation INNTP
  54.  
  55. - init
  56. {
  57.     [super init];
  58.     iNntpHostName = NULL;
  59.  
  60.     iResBufStorage = [[Storage alloc] initCount:0
  61.                    elementSize:(unsigned)LINE_BUFFER_SIZE
  62.                    description:(const char *)"@"];
  63.     
  64.     return self;
  65. }
  66.  
  67.  
  68. - (int)issueCommand:(const char *)command
  69. {
  70.     int         statusCode;
  71.  
  72.     if( !(inntpFile) ) {
  73.         printf(" NNTP connection is not opened yet\n" );
  74.         return 0;
  75.     }
  76.  
  77.     fprintf( inntpFile, "%s\r\n", command );
  78.     fflush( inntpFile );
  79.     fseek( inntpFile, (long)0, SEEK_END );
  80.  
  81.     statusCode = [self _getResponse];
  82.  
  83.     return statusCode;
  84. }
  85.  
  86. - (int)_getResponse
  87. {
  88.     int         statusCode;
  89.    
  90.     fscanf (inntpFile, "%[^\r]\r", iresponse);
  91.     fgetc (inntpFile);                                          /* get \n */
  92.                         /* read response code from textual response line */
  93.     sscanf (iresponse, "%d", &statusCode);
  94.    
  95.     return statusCode;
  96. }
  97.  
  98. - openServer:(const char *)serverName
  99. {
  100.     struct servent    *nntpEnt;
  101.     struct protoent    *nntpProtoEnt;
  102.     struct hostent    *nntpHost;
  103.     struct sockaddr_in    nntpServer;
  104.     int         inCode;
  105.     char         inCodeText[512];
  106.     BOOL        canPost;
  107.  
  108.     // store nntp hostname for reconnecting
  109.     if (iNntpHostName == NULL) {
  110.     iNntpHostName = NXCopyStringBuffer(serverName);
  111.     }
  112.     
  113.     if ((nntpEnt = getservbyname("nntp", "tcp")) == NULL) {
  114.     NXRunAlertPanel(LoStr("News Flash"),LoStr("Cannot find nntp service in 'services' database."),NULL,NULL,NULL);
  115.       return nil;
  116.     }
  117.  
  118.     if ((nntpProtoEnt = getprotobyname(nntpEnt->s_proto)) == NULL) {
  119.     NXRunAlertPanel(LoStr("News Flash"),LoStr("Cannot lookup protocol type."),NULL,NULL,NULL);
  120.     return nil;
  121.     }
  122.  
  123.     if ((nntpSocket = socket(AF_INET, SOCK_STREAM, 
  124.                              nntpProtoEnt->p_proto))== -1) {
  125.     NXRunAlertPanel(LoStr("News Flash"),LoStr("Cannot create socket to news server."),NULL,NULL,NULL);
  126.     return nil;
  127.     }
  128.  
  129.     if ((nntpHost = gethostbyname((char *)serverName)) == NULL) {
  130.     NXRunAlertPanel(LoStr("News Flash"),LoStr("Cannot find address of host %s."),NULL,NULL,NULL, serverName);
  131.     return nil;
  132.     }
  133.  
  134.   nntpServer.sin_family = nntpHost->h_addrtype;
  135.   bcopy(nntpHost->h_addr, &nntpServer.sin_addr, nntpHost->h_length);
  136.   nntpServer.sin_port = nntpEnt->s_port;
  137.   if ((connect(nntpSocket, (struct sockaddr *) &nntpServer,
  138.     sizeof(nntpServer))) == -1) {
  139.     NXRunAlertPanel(LoStr("News Flash"),LoStr("Cannot connect to news server on %s."),NULL,NULL,NULL, serverName);
  140.     return nil;
  141.   }
  142.  
  143.   inntpFile = fdopen(nntpSocket, "r+");
  144.  
  145.   fscanf(inntpFile, "%d %[^\r]\r", &inCode, &inCodeText);
  146.   fgetc(inntpFile);
  147.   fseek(inntpFile, (long)0, SEEK_END);
  148.  
  149.   switch (inCode) {
  150.     case OK_CANPOST:
  151.     case OK_NOPOST:
  152.       canPost = (inCode == OK_CANPOST);
  153.       break;
  154.     default:
  155.       NXRunAlertPanel(LoStr("News Flash"),LoStr("News server on %s responded incorrectly."),NULL,NULL,NULL, serverName);
  156.       return nil;
  157.       break;
  158.   }
  159.  
  160.   return self;
  161. }
  162.  
  163. - reconnectServer
  164. {
  165.     fclose (inntpFile);
  166.  
  167.     if (iNntpHostName == NULL) {
  168.     return nil;
  169.     }
  170.     return ([self openServer:iNntpHostName]);
  171. }
  172.  
  173. - (char *)lastResponse
  174. {
  175.     return iresponse;
  176. }
  177.  
  178. - free
  179. {
  180.     free(iNntpHostName);
  181.     return ([super free]);
  182. }
  183.  
  184. @end
  185.